//
//  AppDelegate.swift
//  CopyWB
//
//  Created by imac on 2017/5/12.
//  Copyright © 2017年 iimac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        window = UIWindow()
        window?.backgroundColor = .white
        
        window?.rootViewController = MainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
    }
}

//模拟从服务器加载 json 文件
extension AppDelegate{
    func loadAppInfo(){
        // 模拟异步
        DispatchQueue.global().async {
            
            // 1> url 
            let jsonURL = Bundle.main.url(forResource: "main.json", withExtension: nil)
            // 2> 将URL的文件转换为data 数据
            let data = NSData(contentsOf: jsonURL!)
            // 3> 写入磁盘
            
            // 获得本地的沙盒路径
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            // 在本地的沙盒路径下写入data数据
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用程序加载完毕, \(jsonPath)")
        }
    }
}

